package com.bwie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.domain.ParkingRecord;
import com.bwie.domain.ParkingSpot;
import com.bwie.mapper.OperationLogMapper;
import com.bwie.mapper.ParkingRecordMapper;
import com.bwie.mapper.ParkingSpotMapper;
import com.bwie.service.OperationLogService;
import com.bwie.service.ParkingRecordService;
import com.bwie.service.ParkingSpotService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.swagger.annotations.Api;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user/user")
@Api(tags = "微服务")
@Slf4j
public class ParkingSpotController {
    @Autowired
    ParkingSpotService parkingSpotService;
    @Autowired
    ParkingSpotMapper parkingSpotMapper;
    @Autowired
    ParkingRecordService parkingRecordService;
    @Autowired
    ParkingRecordMapper parkingRecordMapper;
    @Autowired
    OperationLogService operationLogService;
    @Autowired
    OperationLogMapper operationLogMapper;
    // 车辆入场
    @PostMapping("/parkingRecordAdd")
    public R parkingRecordAdd(@RequestBody ParkingRecord parkingRecord) {
        log.info("车辆入场: {}", parkingRecord.getPlateNumber());

        // 创建停车记录
        ParkingRecord record = new ParkingRecord();
        record.setPlateNumber(parkingRecord.getPlateNumber());
       record.setStatus(0);
       record.setSpotId(parkingRecord.getSpotId()-1);
       record.setEntryTime(new Date());
        int insert = parkingRecordMapper.insert(record);
//        List<ParkingRecord> parkingRecords = parkingRecordMapper.selectList(null);
//        String token = Jwts.builder()
//                .signWith(SignatureAlgorithm.HS256, "123456")
//                .claim("id", record.getId())
//                .compact();
//        record.setToken(token);
        // 更新车位状态
//        spot.setStatus(1);
//        spotMapper.updateById(spot);
//
//        return convertToDTO(record, spot);StringUtils
        return R.OK(insert);
    }

    // 分页查询停车记录
    @PostMapping("/parkingRecordList")
    public R parkingRecordList(@RequestBody PageInfoVo pageInfoVo) {

        Page<ParkingRecord> parkingRecordPage = new Page<>(pageInfoVo.getPageSize(), pageInfoVo.getPageNum());
        QueryWrapper<ParkingRecord> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(pageInfoVo.getKeyWord()))
        {
            wrapper.lambda().eq(ParkingRecord::getPlateNumber,pageInfoVo.getKeyWord());
        }
        if (!StringUtils.isEmpty(pageInfoVo.getDates()))
        {
            wrapper.lambda().eq(ParkingRecord::getEntryTime,pageInfoVo.getDates());
        }
        Page<ParkingRecord> parkingRecordPage1 = parkingRecordMapper.selectPage(parkingRecordPage, wrapper);

        return R.OK(parkingRecordPage1);
    }
    @PostMapping("/parkingRecordById/{id}")
    public R parkingRecordById(@PathVariable("id") Integer id) {
        ParkingRecord parkingRecord = parkingRecordMapper.selectById(id);
        System.out.println("测试-------");
        return R.OK(parkingRecord);
    }

    @PostMapping("/parkingRecordUpdate")
    public R parkingRecordUpdate(@RequestBody ParkingRecord parkingRecord) {
        int i = parkingRecordMapper.updateById(parkingRecord);
        return R.OK(i);
    }
    @PostMapping("/parkingRecordUpdateDEl")
    public R parkingRecordUpdateDEl(@RequestBody ParkingRecord parkingRecord) {
        int i = parkingRecordMapper.updateById(parkingRecord);
        int i1 = parkingRecordMapper.deleteById(parkingRecord);
        return R.OK(i1);
    }
}
